package com.ruoyi.project.system.user.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.reservation.service.IReservationService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import com.ruoyi.project.system.wxUser.domain.WxUser;
import com.ruoyi.project.system.wxUser.service.IWxUserService;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@Controller
public class LoginController extends BaseController
{
	
	@Autowired
	private IUserService userService;
	@Autowired
	private IWxUserService wxUserService;
	
    @GetMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response)
    {
        // 如果是Ajax请求，返回Json字符串。
        if (ServletUtils.isAjaxRequest(request))
        {
            return ServletUtils.renderString(response, "{\"code\":\"1\",\"msg\":\"未登录或登录超时。请重新登录\"}");
        }
        return "login";
    }

    @PostMapping("/login")
    @ResponseBody
    public AjaxResult ajaxLogin(String username, String password, Boolean rememberMe)
    {
    	
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
        Subject subject = SecurityUtils.getSubject();
        try
        {
        	
        	//登录
            subject.login(token);
            
            /*//openid若有值，表示从微信公众号链接过来的认证登录，
        	if(!"".equals(openid)) {
        		User user = new User();
        		user = userService.selectUserByPhoneNumber(username);
                if(user == null) {
                	user = userService.selectUserByLoginName(username);
                }
        		//绑定该openid与用户手机号，方便后面通过微信公众号推送
        		WxUser wxUser = wxUserService.selectWxUserByOpenid(openid);
        		if(wxUser!=null) {
        			wxUser.setUserId(user.getUserId().intValue());
        			wxUser.setTel(user.getPhonenumber());
        			wxUserService.updateWxUser(wxUser);
        			return AjaxResult.success("绑定成功");
        		}
        	}*/
        	
            //如果是学员就直接进入预约界面
            /*User user = null;
            user = iUserService.selectUserByPhoneNumber(username);
            if(user == null) {
            	user = iUserService.selectUserByLoginName(username);
            }
            //如果查到了用户
            if(user!=null) {
            	//判断登录用户是否学员
            	User isUser = iUserService.selectIsRole(user.getUserId(), "学员");
            	if(isUser!=null) {
            		return success("学员");
            	}else {
            		return success();
            	}
            }*/
            return success();
        }
        catch (AuthenticationException e)
        {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage()))
            {
                msg = e.getMessage();
            }
            return error(msg);
        }
    }

    @GetMapping("/unauth")
    public String unauth()
    {
        return "error/unauth";
    }
    
    @GetMapping("/wxBind")
    public ModelAndView wxBind(ModelAndView mav,String OpenID)
    {
    	mav.addObject("openid",OpenID);
    	mav.setViewName("wxBind");
        return mav;
    }
}
